package com.ruoyi.wechat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.wechat.common.Constants;
import com.ruoyi.wechat.domain.IndexRoute;
import com.ruoyi.wechat.domain.Product;
import com.ruoyi.wechat.domain.dto.IndexDTO;
import com.ruoyi.wechat.domain.vo.AjaxResult;
import com.ruoyi.wechat.service.ICategoryService;
import com.ruoyi.wechat.service.IIndexRouteService;
import com.ruoyi.wechat.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 首页配置
 * </p>
 *
 * @author YinHeng
 * @since 2024/3/25
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @Autowired
    private IProductService productService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IIndexRouteService rtService;

    /**
     * 核心业务
     *
     * @return
     */
    @GetMapping("/core-list")
    public AjaxResult indexMain() {
        LambdaQueryWrapper<IndexRoute> rtQuery = new LambdaQueryWrapper<>();
        rtQuery.eq(IndexRoute::getCategory, Constants.IndexRoute.CATEGORY_0.getValue());
        List<IndexRoute> list = rtService.list(rtQuery);
        return AjaxResult.success(list);
    }

    /**
     * 推荐分类
     *
     * @return
     */
    @GetMapping("/rec-list")
    public AjaxResult recList() {
        LambdaQueryWrapper<IndexRoute> rtQuery = new LambdaQueryWrapper<>();
        rtQuery.eq(IndexRoute::getCategory, Constants.IndexRoute.CATEGORY_1.getValue());
        rtQuery.orderByAsc(IndexRoute::getSort);
        List<IndexRoute> list = rtService.list(rtQuery);

        List<List<IndexRoute>> recList = new ArrayList<>();
        List<IndexRoute> itemList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            itemList.add(list.get(i));
            if (itemList.size() % 5 == 0) {
                recList.add(itemList);
                itemList = new ArrayList<>();
            }
        }
        if (itemList.size() > 0) {
            recList.add(itemList);
        }

        return AjaxResult.success(recList);
    }


    /**
     * 热门商品
     *
     * @return
     */
    @GetMapping("/hot-list")
    public AjaxResult hotList() {
        LambdaQueryWrapper<Product> productQuery = new LambdaQueryWrapper<>();
        productQuery.eq(Product::getOnOffStatus, Constants.Product.ON_OFF_STATUS_1.getValue())
                .eq(Product::getCategory, Constants.Product.CATEGORY_3.getValue());
        List<Product> productList = productService.list(productQuery);

        List<IndexDTO> dtoList = new ArrayList<>();
        for (int i = 0; i < productList.size(); i++) {
            Product item = productList.get(i);
            IndexDTO dto = new IndexDTO();
            dto.setItemId(item.getId());
            dto.setName(item.getName());
            dto.setCatImg(item.getCover());
            //设置页面跳转路径
            dto.setPath("/pages/index/product/product");

            dtoList.add(dto);
        }

        return AjaxResult.success(dtoList);

    }


}
